import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        MyStack m = new MyStack();
        m.push(1);
        m.push(2);
        m.top();
        m.pop();
        m.empty();
    }
}

class MyStack {
    Queue<Integer> q1;
    Queue<Integer> q2;

    public MyStack() {
        q1 = new LinkedList();
        q2 = new LinkedList();

    }

    public void push(int x) {
        if (!q2.isEmpty()) {
            q2.offer(x);
        } else if (!q1.isEmpty()) {
            q1.offer(x);
        } else {
            q1.offer(x);
        }

    }

    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!q2.isEmpty()) {
            int size = q2.size();
            for (int i = 0; i < size - 1; i++) {
                q1.offer(q2.poll());
            }
            return q2.poll();
        } else {
            int size = q1.size();
            for (int i = 0; i < size - 1; i++) {
                q2.offer(q1.poll());
            }
            return q1.poll();
        }
    }

    public int top() {
        if (empty()) {
            return -1;
        }

        if (!q2.isEmpty()) {
            int size = q2.size();
            int val = 0;
            for (int i = 0; i < size; i++) {
                val = q2.poll();
                q1.offer(val);
            }
            return val;
        } else {
            int size = q1.size();
            int val = 0;
            for (int i = 0; i < size; i++) {
                val = q1.poll();
                q2.offer(val);
            }
            return val;
        }
    }

    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();

    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */
class MyQueue {
    Stack<Integer> s1;
    Stack<Integer> s2;


    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }

    public void push(int x) {
        if (!s1.isEmpty()) {
            s1.push(x);

        } else if (!s2.isEmpty()) {
            s2.push(x);

        } else {
            s1.push(x);
        }
    }

    public int pop() {
        if(empty())
            return -1;
        if(!s1.isEmpty()){
            int size=s1.size();
            for (int i = 0; i < size-1; i++) {
                s2.push(s1.pop());
            }
            return s1.pop();
        }else {
            int size=s2.size();
            for (int i = 0; i < size-1; i++) {
                s1.push(s2.pop());
            }
            return s2.pop();
        }

    }
    public int peek() {
        if (empty())
            return -1;
        if (!s1.isEmpty()) {
            int size = s1.size();
            int val=0;
            for (int i = 0; i < size; i++) {
                val=s1.pop()
                s2.push(val);
            }
            return val;
        } else {
            int size = s2.size();
            int val=0;
            for (int i = 0; i < size - 1; i++) {
                val=s2.pop();
                s1.push(val);
            }
            return val;

        }
    }

    public boolean empty() {
return s1.isEmpty()&&s2.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */